package com.njrsrc.android.app;

import java.util.ArrayList;
import java.util.List;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ListView;
import android.widget.Toast;

import com.njrsrc.R;
import com.njrsrc.android.adapters.JobListAdapter;
import com.njrsrc.android.bean.JobInfo;
import com.njrsrc.android.bean.UserInfo;
import com.njrsrc.android.library.ptr.PullToRefreshBase;
import com.njrsrc.android.library.ptr.PullToRefreshBase.Mode;
import com.njrsrc.android.library.ptr.PullToRefreshBase.OnRefreshListener;
import com.njrsrc.android.library.ptr.PullToRefreshListView;
import com.njrsrc.android.library.vpi.BaseSampleActivity;
import com.njrsrc.android.tasks.JobCollectTask;
import com.njrsrc.android.views.TitleBar;
import com.njrsrc.common.url.RequestUrlProvider;
import com.njrsrc.common.utils.Helpers;
import com.njrsrc.common.utils.MsgID;

/**
 * 工作收藏/申请记录/找工作列表共用页面
 * 
 * @author Administrator
 * 
 */
public class JobCollectActivity extends BaseSampleActivity implements OnClickListener {
	private TitleBar mBar;
	private UserInfo userInfo;
	private ProgressDialog progressDialog;
	private PullToRefreshListView jobListView;
	private JobListAdapter adapter;
	private int PAGE_ID = 1;
	/** 每页显示的条数 */
	private static final int COUNT = 10;
	private boolean loadingEnd = false;
	private ArrayList<JobInfo> jobList;
	private String requestUrl;
	private String from;
	private String area_code;
	private String industry_code;
	private String position_code;
	private String keyWord;

	private Handler mHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case MsgID.JOB_COLLECT_QUERY_FAIL:
				// 工作收藏查询失败
				Helpers.closeProgressDialog(progressDialog);
				break;
			case MsgID.JOB_COLLECT_QUERY_ERROR:
				// 工作收藏查询错误
				Helpers.closeProgressDialog(progressDialog);
				Helpers.showToast(JobCollectActivity.this, R.string.connectError, Toast.LENGTH_SHORT);
				break;
			case MsgID.JOB_COLLECT_QUERY_SUCCESS:
				// 工作收藏查询成功
				Helpers.closeProgressDialog(progressDialog);
				jobListView.onRefreshComplete();
				adapter.setData(jobList);
				adapter.notifyDataSetChanged();
				clear();
				break;

			default:
				break;
			}
			super.handleMessage(msg);
		}
	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_job_collect);
		initView();
	}

	private void initView() {
		mBar = (TitleBar) findViewById(R.id.activity_title_bar);
		from = getIntent().getStringExtra("from");
		area_code = getIntent().getStringExtra("area_code");
		industry_code = getIntent().getStringExtra("industry_code");
		position_code = getIntent().getStringExtra("position_code");
		keyWord = getIntent().getStringExtra("keyWord");
		if (from.equals("collect")) {
			// 工作收藏
			requestUrl = RequestUrlProvider.JOB_FAVORITE_LIST;
			mBar.setTitleText("工作收藏");
		} else if (from.equals("record")) {
			// 申请记录
			requestUrl = RequestUrlProvider.JOB_APPLIED_LIST;
			mBar.setTitleText("职位申请记录");
		} else if (from.equals("search")) {
			// 找工作
			requestUrl = RequestUrlProvider.JOB_LIST;
			mBar.setTitleText("找工作");
		}
		mBar.back.setOnClickListener(this);
		jobListView = (PullToRefreshListView) findViewById(R.id.job_listView);
		jobListView.setScrollingWhileRefreshingEnabled(!jobListView.isScrollingWhileRefreshingEnabled());
		jobListView.setMode(Mode.PULL_FROM_END);
		jobListView.setOnRefreshListener(new OnRefreshListener<ListView>() {
			@Override
			public void onRefresh(PullToRefreshBase<ListView> refreshView) {
				if (!loadingEnd) {
					initData();
				}
			}
		});
		adapter = new JobListAdapter(JobCollectActivity.this, from);
		jobListView.setAdapter(adapter);
		initData();
	}

	private void initData() {
		List<String> params = new ArrayList<String>();
		params.add(AppApplication.mToken);
		params.add(AppApplication.mUserID);
		if (from.equals("search")) {
			params.add(industry_code);
			params.add(position_code);
			params.add(area_code);
			// 公司性质码
			params.add("");
			// 工作发布时间范围。单位：天。若指定此参数，则查找N天内的工作
			params.add("");
			// 搜索关键字
			params.add(keyWord);
			// 公司ID。查找指定公司发布的工作。
			params.add("");
			// 若指定此参数，则返回工作ID在since_id之前的工作，0表示忽略此参数，默认为0。
			params.add("0");
			params.add(PAGE_ID + "");
			params.add(COUNT + "");
		} else {
			params.add("0");
			params.add(PAGE_ID + "");
			params.add(COUNT + "");
		}
		String url = Helpers.combinaStr(requestUrl, params);
		userInfo = new UserInfo();
		jobList = new ArrayList<JobInfo>();
		Object[] args = new Object[5];
		args[0] = mHandler;
		args[1] = url;
		args[2] = userInfo;
		args[3] = jobList;
		args[4] = from;
		progressDialog = Helpers.showProgressDialog(JobCollectActivity.this, R.string.loading);
		new JobCollectTask().execute(args);
	}

	private void clear() {
		if (jobList == null || jobList.size() == 0 || jobList.size() < COUNT) {
			loadingEnd = true;
			jobListView.setMode(Mode.DISABLED);
		} else {
			PAGE_ID++;
		}
		jobList.clear();
		jobList = null;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.tb_ibtn_back:
			finish();
			break;
		default:
			break;
		}
	}
}